{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Model of the Fed's View on Inflation\n",
    "\n",
    "### Thomas Hasenzagl$^1$, Filippo Pellegrino$^2$, Lucrezia Reichlin$^3$, Giovanni Ricco$^4$\n",
    "\n",
    "$^1$ University of Minnesota <br>\n",
    "$^2$ London School of Economics and Political Science <br>\n",
    "$^3$ London Business School, Now-Casting Economics, and CEPR <br>\n",
    "$^4$ University of Warwick and OFCE-SciencesPo <br>\n",
    "\n",
    "\n",
    "#### Notebook description\n",
    "\n",
    "This notebook generates the charts from the in-sample estimation exercise from the paper \"A Model of the Fed's View on Inflation\". Before running this notebook you have to run `user_main.jl` using `run_type=1`. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialization: libraries and initial settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Load libraries -----\n",
    "\n",
    "# Built-in\n",
    "using Colors, CSV, Dates, DataFrames, Distributions, FileIO, JLD, ORCA, XLSX, KernelDensity, PlotlyJS;\n",
    "pltjs = PlotlyJS\n",
    "\n",
    "# Custom\n",
    "include(\"MetropolisWithinGibbs.jl\");\n",
    "using Main.MetropolisWithinGibbs;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Load results from output file -----\n",
    "\n",
    "res           = load(\"res_iis.jld\");\n",
    "nDraws        = res[\"nDraws\"];\n",
    "burnin        = res[\"burnin\"];\n",
    "σʸ            = res[\"σʸ\"]';\n",
    "date          = res[\"date\"];\n",
    "data          = res[\"data\"] .* σʸ';\n",
    "distr_α       = res[\"distr_α\"];\n",
    "chain_θ_bound = res[\"chain_θ_bound\"]; # d, Z, R, c, T, Q, λ, ρ, total\n",
    "MNEMONIC      = res[\"MNEMONIC\"];\n",
    "\n",
    "data[ismissing.(data)] .= NaN;\n",
    "\n",
    "par_ind  = res[\"par_ind\"];\n",
    "\n",
    "par_size = SizeParSsm(sum(par_ind.d),\n",
    "                                 sum(sum(par_ind.Z)),\n",
    "                                 sum(sum(par_ind.R)),\n",
    "                                 sum(par_ind.c),\n",
    "                                 sum(sum(par_ind.T)),\n",
    "                                 sum(sum(par_ind.Q)),\n",
    "                                 sum(sum(par_ind.λ)),\n",
    "                                 sum(sum(par_ind.ρ)),\n",
    "                                 sum(par_ind.d) + sum(sum(par_ind.Z)) + sum(sum(par_ind.R)) +\n",
    "                                    sum(par_ind.c) + sum(sum(par_ind.T)) + sum(sum(par_ind.Q)) +\n",
    "                                    sum(sum(par_ind.λ)) + sum(sum(par_ind.ρ)));\n",
    "\n",
    "# ----- Set titles and scales -----\n",
    "\n",
    "titles = [\"Real GDP\", \"Employment\", \"Unemployment rate\", \"Oil price\", \"CPI inflation\", \"Core CPI inflation\", \"UoM: Expected inflation\", \"SPF: Expected inflation\"];\n",
    "scales = [\"Bil. Chn. 2009\\$\", \"Thous.\", \"Percent\", \"\\$/Barrel\", \"Percent\", \"Percent\", \"Percent\", \"Percent\"];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Get trends and cycles -----\n",
    "\n",
    "n          = size(data)[2];\n",
    "TT         = size(distr_α, 2);\n",
    "ind_trends = [collect(9:3:size(distr_α)[1])[1:4]; collect(25:3:size(distr_α)[1])]; # There is no index for CPI inflation, please use if-else\n",
    "ind_cycles = [collect(7:3:size(distr_α)[1])[1:4]; 19; 21; collect(23:3:size(distr_α)[1])];\n",
    "\n",
    "Zᵖ = [ones(1, nDraws[2]-burnin[2]); \n",
    "      chain_θ_bound[1:7, burnin[2]+1:end]];\n",
    "\n",
    "Zᵖ₂ = [zeros(1, nDraws[2]-burnin[2]); \n",
    "       chain_θ_bound[8:14, burnin[2]+1:end]];\n",
    "\n",
    "Zᵖ₃ = [zeros(6, nDraws[2]-burnin[2]); \n",
    "       chain_θ_bound[15:16, burnin[2]+1:end]];\n",
    "\n",
    "Zᵉ = [zeros(3, nDraws[2]-burnin[2]); \n",
    "      ones(1, nDraws[2]-burnin[2]); \n",
    "      chain_θ_bound[17:20, burnin[2]+1:end]];\n",
    "\n",
    "Zᵉ₂ = [zeros(3, nDraws[2]-burnin[2]); \n",
    "       zeros(1, nDraws[2]-burnin[2]); \n",
    "       chain_θ_bound[21:24, burnin[2]+1:end]]\n",
    "\n",
    "Zᵗ = [zeros(4, nDraws[2]-burnin[2]); \n",
    "      (1 ./ σʸ[end-3:end]) .* ones(4, nDraws[2]-burnin[2])];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PC = zeros(n, TT, nDraws[2]-burnin[2]);\n",
    "EP = zeros(n, TT, nDraws[2]-burnin[2]);\n",
    "T  = zeros(n, TT, nDraws[2]-burnin[2]);\n",
    "\n",
    "for i=1:nDraws[2]-burnin[2]    \n",
    "    PC[:, :, i] = (Zᵖ[:, i] .* distr_α[1, :, i]') .+ \n",
    "                  (Zᵖ₂[:, i] .* distr_α[2, :, i]') .+\n",
    "                  (Zᵖ₃[:, i] .* distr_α[3, :, i]');\n",
    "    \n",
    "    EP[:, :, i] = (Zᵉ[:, i] .* distr_α[4, :, i]') .+ \n",
    "                  (Zᵉ₂[:, i] .* distr_α[5, :, i]');\n",
    "\n",
    "    T[:, :, i]  = Zᵗ[:, i] .* distr_α[6, :, i]';\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iC = distr_α[ind_cycles, :, :];\n",
    "iT = distr_α[ind_trends, :, :];\n",
    "\n",
    "PCᵐ = median(PC, dims=3)[:, :, 1]' .* σʸ';\n",
    "EPᵐ = median(EP, dims=3)[:, :, 1]' .* σʸ';\n",
    "Tᵐ  = median(T, dims=3)[:, :, 1]' .* σʸ';\n",
    "iCᵐ = median(iC, dims=3)[:, :, 1]' .* σʸ';\n",
    "iTᵐ = median(iT, dims=3)[:, :, 1]' .* [σʸ[1:4]' σʸ[7:8]'];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PC95 = zeros(size(PCᵐ));\n",
    "PC05 = zeros(size(PCᵐ));\n",
    "PC84 = zeros(size(PCᵐ));\n",
    "PC16 = zeros(size(PCᵐ));\n",
    "\n",
    "EP95 = zeros(size(EPᵐ));\n",
    "EP05 = zeros(size(EPᵐ));\n",
    "EP84 = zeros(size(EPᵐ));\n",
    "EP16 = zeros(size(EPᵐ));\n",
    "\n",
    "T95 = zeros(size(Tᵐ));\n",
    "T05 = zeros(size(Tᵐ));\n",
    "T84 = zeros(size(Tᵐ));\n",
    "T16 = zeros(size(Tᵐ));\n",
    "\n",
    "iC95 = zeros(size(iCᵐ));\n",
    "iC05 = zeros(size(iCᵐ));\n",
    "iC84 = zeros(size(iCᵐ));\n",
    "iC16 = zeros(size(iCᵐ));\n",
    "\n",
    "iT95 = zeros(size(iTᵐ));\n",
    "iT05 = zeros(size(iTᵐ));\n",
    "iT84 = zeros(size(iTᵐ));\n",
    "iT16 = zeros(size(iTᵐ));\n",
    "\n",
    "for i=1:size(data, 2)\n",
    "    for j=1:size(data, 1)\n",
    "        \n",
    "        PC95[j, i] = quantile(PC[i, j, :], 0.95) .* σʸ[i];\n",
    "        PC05[j, i] = quantile(PC[i, j, :], 0.05) .* σʸ[i];\n",
    "        PC84[j, i] = quantile(PC[i, j, :], 0.84) .* σʸ[i];\n",
    "        PC16[j, i] = quantile(PC[i, j, :], 0.16) .* σʸ[i];\n",
    "        \n",
    "        EP95[j, i] = quantile(EP[i, j, :], 0.95) .* σʸ[i];\n",
    "        EP05[j, i] = quantile(EP[i, j, :], 0.05) .* σʸ[i];\n",
    "        EP84[j, i] = quantile(EP[i, j, :], 0.84) .* σʸ[i];\n",
    "        EP16[j, i] = quantile(EP[i, j, :], 0.16) .* σʸ[i];\n",
    "\n",
    "        T95[j, i] = quantile(T[i, j, :], 0.95) .* σʸ[i];\n",
    "        T05[j, i] = quantile(T[i, j, :], 0.05) .* σʸ[i];\n",
    "        T84[j, i] = quantile(T[i, j, :], 0.84) .* σʸ[i];\n",
    "        T16[j, i] = quantile(T[i, j, :], 0.16) .* σʸ[i];\n",
    "        \n",
    "        iC95[j, i] = quantile(iC[i, j, :], 0.95) .* σʸ[i];\n",
    "        iC05[j, i] = quantile(iC[i, j, :], 0.05) .* σʸ[i];\n",
    "        iC84[j, i] = quantile(iC[i, j, :], 0.84) .* σʸ[i];\n",
    "        iC16[j, i] = quantile(iC[i, j, :], 0.16) .* σʸ[i];\n",
    "        \n",
    "\n",
    "        if i < 5\n",
    "            iT95[j, i] = quantile(iT[i, j, :], 0.95) .* σʸ[i];\n",
    "            iT05[j, i] = quantile(iT[i, j, :], 0.05) .* σʸ[i];\n",
    "            iT84[j, i] = quantile(iT[i, j, :], 0.84) .* σʸ[i];\n",
    "            iT16[j, i] = quantile(iT[i, j, :], 0.16) .* σʸ[i];\n",
    "        elseif i > 6\n",
    "            iT95[j, i-2] = quantile(iT[i-2, j, :], 0.95) .* σʸ[i];\n",
    "            iT05[j, i-2] = quantile(iT[i-2, j, :], 0.05) .* σʸ[i];\n",
    "            iT84[j, i-2] = quantile(iT[i-2, j, :], 0.84) .* σʸ[i];\n",
    "            iT16[j, i-2] = quantile(iT[i-2, j, :], 0.16) .* σʸ[i];\n",
    "        end\n",
    "    end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Dates: add h dates to date -----\n",
    "\n",
    "date=[date[i] for i=1:length(date)];\n",
    "max_h = size(PCᵐ)[1] - size(date)[1];\n",
    "\n",
    "for hz=1:max_h\n",
    "    \n",
    "    last_month = Dates.month(date[end]);\n",
    "    last_year  = Dates.year(date[end]);\n",
    "    new_month  = copy(last_month) + 3;\n",
    "    new_year   = copy(last_year);\n",
    "    \n",
    "    if last_month + 1 > 12\n",
    "        new_year  += 1;\n",
    "        new_month  = 3;\n",
    "    end\n",
    "    \n",
    "    new_entry = Dates.lastdayofquarter(Date(new_year, new_month, 1));\n",
    "    date      = vcat(date, DateTime(new_entry));\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Colour\n",
    "\n",
    "c1 = \"rgba(0, 48, 158, .75)\"; #\"rgba(0, 0, 158, .7)\";\n",
    "c2 = \"rgba(255, 0, 0, .75)\";\n",
    "c3 = \"rgba(255, 190, 0, .75)\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "---\n",
    "\n",
    "### Charts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Introduction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "figures   = Array{Any}(undef, 2);\n",
    "ind_start = 49;\n",
    "\n",
    "vv = [5, 5];\n",
    "\n",
    "for i=1:2\n",
    "    \n",
    "    if i == 1\n",
    "        trace1 = pltjs.bar(x=date, y=PCᵐ[1:end-max_h, vv[i]], name=\"Business cycle\", marker_color=c1, showlegend=true, legendgroup=\"g1\");\n",
    "        trace2 = pltjs.bar(x=date, y=EPᵐ[1:end-max_h, vv[i]], name=\"Energy price cycle\", marker_color=c2, showlegend=true, legendgroup=\"g2\");\n",
    "        trace3 = pltjs.bar(x=date, y=iCᵐ[1:end-max_h, vv[i]], name=\"Idiosyncratic cycle\", marker_color=c3, showlegend=true, legendgroup=\"g3\");\n",
    "        trace4 = pltjs.scatter(x=date[1:end-max_h], y=PCᵐ[1:end-max_h, vv[i]]+EPᵐ[1:end-max_h, vv[i]]+iCᵐ[1:end-max_h, vv[i]], name=\"Cycle\", mode=\"lines\", line=attr(width=1.4, color=\"black\", dash=\"dot\"), showlegend=true, legendgroup=\"g4\");\n",
    "        \n",
    "        databar = [trace1, trace2, trace3, trace4];    \n",
    "        layout  = pltjs.Layout(title=string(\"Cycle: \", titles[vv[i]]), titlefont_size=10,\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", nticks=6, mirror=true, titlefont=attr(size=10), title=scales[vv[i]]),\n",
    "                               barmode=\"relative\", \n",
    "                               bargap=0,\n",
    "                               bargroupgap=0);\n",
    "        \n",
    "        figures[i] = pltjs.plot(databar, layout);\n",
    "\n",
    "    else\n",
    "        trace0 = pltjs.scatter(x=date[1:end-max_h], y=data[1:end-max_h, vv[i]], name=\"Data\", mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=true, legendgroup=\"g1\");\n",
    "        trace1 = pltjs.scatter(x=date[1:end-max_h], y=Tᵐ[1:end-max_h, vv[i]], name=\"Trend\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(x=date[1:end-max_h], y=T95[1:end-max_h, vv[i]], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=true, legendgroup=\"g4\");\n",
    "        trace3 = pltjs.scatter(x=date[1:end-max_h], y=T05[1:end-max_h, vv[i]], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(x=date[1:end-max_h], y=T84[1:end-max_h, vv[i]], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=true, legendgroup=\"g3\");\n",
    "        trace5 = pltjs.scatter(x=date[1:end-max_h], y=T16[1:end-max_h, vv[i]], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(x=date[1:end-max_h], y=Tᵐ[1:end-max_h, vv[i]], name=\"Trend\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=true, legendgroup=\"g2\");\n",
    "\n",
    "        layout  = pltjs.Layout(title=string(\"Trend: \", titles[vv[i]]), titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[vv[i]]));\n",
    "\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6; trace0], layout);\n",
    "    end\n",
    "end\n",
    "\n",
    "fig = [figures[1]; figures[2]];\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"barmode\"]  = \"relative\";\n",
    "fig.plot.layout[\"bargap\"]   = 0.02;\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:2\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "fig.plot.layout[\"legend\"] = attr(y=-0.1, x=0.176, font=attr(size=10), orientation=\"h\")\n",
    "\n",
    "savefig(fig, \"introduction.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Historical decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "figures = Array{Any}(undef, 8);\n",
    "\n",
    "for i=1:8\n",
    "    trace1 = pltjs.bar(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, i], name=\"Business cycle\", marker_color=c1, showlegend=i==1);\n",
    "    trace2 = pltjs.bar(x=date[1:end-max_h], y=EPᵐ[1:end-max_h, i], name=\"Energy price cycle\", marker_color=c2, showlegend=i==1);\n",
    "    trace3 = pltjs.bar(x=date[1:end-max_h], y=iCᵐ[1:end-max_h, i], name=\"Idiosyncratic cycle\", marker_color=c3, showlegend=i==1);\n",
    "    trace4 = pltjs.scatter(x=date[1:end-max_h], y=PCᵐ[1:end-max_h, i]+EPᵐ[1:end-max_h, i]+iCᵐ[1:end-max_h, i], name=\"Total cycle\", mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=i==1)\n",
    "    \n",
    "    databar = [trace1, trace2, trace3, trace4];    \n",
    "    layout  = pltjs.Layout(;title=titles[i], titlefont_size=10,\n",
    "                           xaxis=attr(tickfont_size=14, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90),\n",
    "                           yaxis=attr(tickfont_size=14, showgrid=true, linecolor=\"black\", mirror=true, titlefont=attr(size=14), title=scales[i]),\n",
    "                           barmode=\"relative\", \n",
    "                           bargap=0,\n",
    "                           bargroupgap=0);\n",
    "\n",
    "    figures[i] = pltjs.plot(databar, layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]; figures[5] figures[6]; figures[7] figures[8]];\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"barmode\"] = \"relative\";\n",
    "fig.plot.layout[\"bargap\"]  = 0.02;\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 800;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:size(data, 2)\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 20;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.1, x=0.075, font=attr(size=14))\n",
    "\n",
    "savefig(fig, \"historical_decomposition.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Posteriors: key parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Frequency -----\n",
    "\n",
    "# - Chart\n",
    "figures    = Array{Any}(undef, 2)\n",
    "titles_sub = [\"Frequency, BC cycle\",\n",
    "              \"Frequency, EP cycle\"];\n",
    "\n",
    "for i=1:2\n",
    "    \n",
    "    # Frequency\n",
    "    k             = kde(chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+i, :]);\n",
    "    kde_x         = k.x;\n",
    "    x_prior       = collect(0:0.01:pi);\n",
    "    kde_prior     = pdf(Uniform(0, pi), x_prior);\n",
    "    kde_posterior = k.density;\n",
    "    \n",
    "    \n",
    "    trace1 = pltjs.scatter(x=x_prior, y=kde_prior, line=attr(width=1.4, color=c2), name=\"Prior\", showlegend=i==1);\n",
    "    trace2 = pltjs.scatter(x=kde_x, y=kde_posterior, line=attr(width=1.4, color=c1), name=\"Posterior\", showlegend=i==1);\n",
    "\n",
    "    layout = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, range=[0, pi]),\n",
    "                           yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Density\"));\n",
    "\n",
    "    figures[i] = pltjs.plot([trace1; trace2], layout);\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures[1]; figures[2]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:2\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.1, x=0.375, font=attr(size=10));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Persistence -----\n",
    "\n",
    "# - Chart\n",
    "figures2    = Array{Any}(undef, 2)\n",
    "titles_sub = [\"Persistence, BC cycle\",\n",
    "              \"Persistence, EP cycle\"];\n",
    "\n",
    "for i=1:2\n",
    "    \n",
    "    # Persistence\n",
    "    k             = kde(chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+par_size.λ+i, :]);\n",
    "    kde_x         = k.x;\n",
    "    x_prior       = collect(0:0.01:0.970);\n",
    "    kde_prior     = pdf(Uniform(0, pi), x_prior);\n",
    "    kde_posterior = k.density;\n",
    "    \n",
    "    \n",
    "    trace1 = pltjs.scatter(x=x_prior, y=kde_prior, line=attr(width=1.4, color=c2), name=\"Prior\", showlegend=false);\n",
    "    trace2 = pltjs.scatter(x=kde_x, y=kde_posterior, line=attr(width=1.4, color=c1), name=\"Posterior\", showlegend=false);\n",
    "\n",
    "    layout = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, range=[0, 0.970]),\n",
    "                           yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Density\"));\n",
    "\n",
    "    figures2[i] = pltjs.plot([trace1; trace2], layout);\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures2[1]; figures2[2]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:2\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.1, x=0.375, font=attr(size=10));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Variance -----\n",
    "\n",
    "# - Chart\n",
    "figures_var    = Array{Any}(undef, 3)\n",
    "titles_sub = [\"Variance, BC cycle\", \n",
    "              \"Variance, EP cycle\", \n",
    "              \"Variance, Common trend\"];\n",
    "\n",
    "for i=1:3\n",
    "    k             = kde(chain_θ_bound[par_size.R+par_size.d+par_size.Z+i, :]);\n",
    "    kde_x         = k.x;\n",
    "    x_prior       = collect(0:0.01:1.5);\n",
    "    kde_prior     = pdf(InverseGamma(3, 1), x_prior);\n",
    "    kde_posterior = k.density;\n",
    "\n",
    "    trace1 = pltjs.scatter(x=x_prior, y=kde_prior, line=attr(width=1.4, color=c2), name=\"Prior\", showlegend=false);\n",
    "    trace2 = pltjs.scatter(x=kde_x, y=kde_posterior, line=attr(width=1.4, color=c1), name=\"Posterior\", showlegend=false);\n",
    "\n",
    "    layout = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, range=[0, 1.5]),\n",
    "                           yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Density\"));\n",
    "\n",
    "    figures_var[i] = pltjs.plot([trace1; trace2], layout);\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures_var[1]; figures_var[2]; figures_var[3]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 800;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:3\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.06, x=0.375, font=attr(size=10));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "empty = pltjs.plot();\n",
    "fig = [figures[1] figures[2] figures2[1] figures2[2]; figures_var[1] figures_var[2] figures_var[3] empty];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 1000;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:7\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.1, x=0.42, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"posterior.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Loadings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Loadings -----\n",
    "\n",
    "# - Chart\n",
    "figures_var = Array{Any}(undef, 4)\n",
    "titles_sub = [\"Coefficients for CPI inflation, Business cycle\", \n",
    "                   \"Coefficients for Core CPI inflation, Business cycle\",\n",
    "                   \"Coefficients for CPI inflation, Energy price cycle\", \n",
    "                   \"Coefficients for Core CPI inflation, Energy price cycle\"];\n",
    "\n",
    "for i=1:4\n",
    "    if i < 3\n",
    "        k = kde(Zᵖ[5+i-1, :]);\n",
    "    else\n",
    "        k = kde(Zᵉ[5+i-3, :]);\n",
    "    end\n",
    "    \n",
    "    kde_x         = k.x;\n",
    "    x_prior       = collect(-5:0.1:5);\n",
    "    kde_prior     = pdf(Normal(0, 1000), x_prior);\n",
    "    kde_posterior = k.density;\n",
    "\n",
    "    trace1 = pltjs.scatter(x=x_prior, y=kde_prior, line=attr(width=1.4, color=c2), name=\"Prior\", showlegend=i==1);\n",
    "    trace2 = pltjs.scatter(x=kde_x, y=kde_posterior, line=attr(width=1.4, color=c1), name=\"Posterior\", showlegend=i==1);\n",
    "\n",
    "    layout = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, range=[-2, 2]),\n",
    "                           yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Density\"));\n",
    "\n",
    "    figures_var[i] = pltjs.plot([trace1; trace2], layout);\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures_var[1] figures_var[2]; figures_var[3] figures_var[4]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:4\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.05, x=0.4, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"loadings.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Trends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figures=Array{Any}(undef, 4)\n",
    "\n",
    "cbo_gdp_potential = CSV.read(\"potential.csv\")[:output];\n",
    "cbo_nairu = CSV.read(\"potential.csv\")[:nairu];\n",
    "cbo_gdp_potential[ismissing.(cbo_gdp_potential)] .= NaN;\n",
    "cbo_nairu[ismissing.(cbo_nairu)] .= NaN;\n",
    "\n",
    "for i=1:4\n",
    "    \n",
    "    if i == 1\n",
    "        trace_cbo = pltjs.scatter(x=date[1:end-max_h], y=cbo_gdp_potential, line=attr(width=1.4, color=c2), name=\"CBO\", showlegend=true);\n",
    "    elseif i == 3\n",
    "        trace_cbo = pltjs.scatter(x=date[1:end-max_h], y=cbo_nairu, line=attr(width=1.4, color=c2), name=\"CBO\", showlegend=false);\n",
    "    end\n",
    "    \n",
    "    trace0 = pltjs.scatter(;x=date[1:end-max_h], y=data[1:end-max_h, i], name=\"Data\", mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=i==4);\n",
    "    trace1 = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, i], name=\"Trend\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "    trace2 = pltjs.scatter(;x=date[1:end-max_h], y=iT95[1:end-max_h, i], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=i==4);\n",
    "    trace3 = pltjs.scatter(;x=date[1:end-max_h], y=iT05[1:end-max_h, i], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "    trace4 = pltjs.scatter(;x=date[1:end-max_h], y=iT84[1:end-max_h, i], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=i==4);\n",
    "    trace5 = pltjs.scatter(;x=date[1:end-max_h], y=iT16[1:end-max_h, i], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "    trace6 = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, i], name=\"Trend\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=i==4);\n",
    "\n",
    "     layout  = pltjs.Layout(;title=titles[i], titlefont=attr(size=12),\n",
    "                            xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                            yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[i]));\n",
    "    \n",
    "     if i == 1 || i == 3\n",
    "         figures[i] = pltjs.plot([trace_cbo; trace1; trace2; \n",
    "                                  trace1; trace3; \n",
    "                                  trace1; trace4;\n",
    "                                  trace1; trace5; \n",
    "                                  trace6; trace0], layout);\n",
    "\n",
    "     else\n",
    "         figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                  trace1; trace3; \n",
    "                                  trace1; trace4;\n",
    "                                  trace1; trace5; \n",
    "                                  trace6; trace0], layout);\n",
    "    end\n",
    "end\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]];\n",
    "\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:4\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.10, x=0.25, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"trends_real.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "figures=Array{Any}(undef, 4)\n",
    "\n",
    "\n",
    "for i=5:8\n",
    "    \n",
    "     trace0 = pltjs.scatter(;x=date[1:end-max_h], y=data[1:end-max_h, i], name=\"Data\", mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=i==5);\n",
    "     trace1 = pltjs.scatter(;x=date[1:end-max_h], y=Tᵐ[1:end-max_h, i], name=\"Trend\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "     trace2 = pltjs.scatter(;x=date[1:end-max_h], y=T95[1:end-max_h, i], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=i==5);\n",
    "     trace3 = pltjs.scatter(;x=date[1:end-max_h], y=T05[1:end-max_h, i], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "     trace4 = pltjs.scatter(;x=date[1:end-max_h], y=T84[1:end-max_h, i], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=i==5);\n",
    "     trace5 = pltjs.scatter(;x=date[1:end-max_h], y=T16[1:end-max_h, i], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "     trace6 = pltjs.scatter(;x=date[1:end-max_h], y=Tᵐ[1:end-max_h, i], name=\"Trend\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=i==5);\n",
    "\n",
    "     layout  = pltjs.Layout(;title=titles[i], titlefont=attr(size=12),\n",
    "                            xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                            yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[i], range=[-2,8]));\n",
    "\n",
    "     figures[i-4] = pltjs.plot([trace1; trace2; \n",
    "                              trace1; trace3; \n",
    "                              trace1; trace4;\n",
    "                              trace1; trace5; \n",
    "                              trace6; trace0], layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1]; figures[2]; figures[3]; figures[4]];\n",
    "\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 800;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:4\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.10, x=0.3, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"trends_infl.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "long_term_exp = CSV.read(\"INFCPI10YR.csv\")[:INFCPI10YR];\n",
    "long_term_exp[ismissing.(long_term_exp)] .= NaN;\n",
    "\n",
    "i = 5;\n",
    "\n",
    "trace0a = pltjs.scatter(;x=date[1:end-max_h], y=long_term_exp[1:size(date)[1]-max_h], name=\"SPF: 10y exp.\", mode=\"lines\", line=attr(width=1.4, color=\"red\"), showlegend=true);\n",
    "trace0b = pltjs.scatter(;x=date[1:end-max_h], y=data[1:end-max_h, i], name=titles[5], mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=true);\n",
    "trace1  = pltjs.scatter(;x=date[1:end-max_h], y=Tᵐ[1:end-max_h, i], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "trace2  = pltjs.scatter(;x=date[1:end-max_h], y=T95[1:end-max_h, i], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=true);\n",
    "trace3  = pltjs.scatter(;x=date[1:end-max_h], y=T05[1:end-max_h, i], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "trace4  = pltjs.scatter(;x=date[1:end-max_h], y=T84[1:end-max_h, i], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=true);\n",
    "trace5  = pltjs.scatter(;x=date[1:end-max_h], y=T16[1:end-max_h, i], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "trace6  = pltjs.scatter(;x=date[1:end-max_h], y=Tᵐ[1:end-max_h, i], name=\"Trend\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=true);\n",
    "\n",
    "layout  = pltjs.Layout(;title=\"Long-term expectations for CPI inflation\", titlefont=attr(size=12),\n",
    "                        xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                        yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[i]));\n",
    "\n",
    "fig = [pltjs.plot([trace1; trace2; \n",
    "                       trace1; trace3; \n",
    "                       trace1; trace4;\n",
    "                       trace1; trace5; \n",
    "                       trace6; trace0a; trace0b], layout)]\n",
    "\n",
    "\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 300;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.22, x=0.185, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"trends_infl_long_term_exp.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Parametric spectrum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Spectral density distribution -----\n",
    "\n",
    "num_s(ω, λ, ρ, σ²) = σ² .* (1 .+ ρ.^2 .- 2*ρ.*cos.(ω).*cos.(λ));\n",
    "den_s(ω, λ, ρ)     = (2*pi) .* (1 .+ ρ.^4 .+ 4*(ρ^2).*(cos.(ω).^2) .- 4*ρ.*(1 .+ ρ.^2).*cos.(ω).*cos.(λ) .+ 2*(ρ.^2).*cos.(2*λ));\n",
    "s(ω, λ, ρ, σ²)     = num_s(ω, λ, ρ, σ²)./den_s(ω, λ, ρ);\n",
    "\n",
    "ω       = collect(0:0.01:pi);\n",
    "s_distr = zeros(length(ω), 3, nDraws[2]-burnin[2]);\n",
    "\n",
    "for i=1:3\n",
    "    \n",
    "    # parameters\n",
    "    if i<3\n",
    "        λ  = chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+i, :];\n",
    "        ρ  = chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+par_size.λ+i, :];\n",
    "        σ² = chain_θ_bound[par_size.R+par_size.d+par_size.Z+i, :];\n",
    "    else\n",
    "        λ  = chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+2+4, :];\n",
    "        ρ  = chain_θ_bound[par_size.R+par_size.d+par_size.Z+par_size.Q+par_size.c+par_size.T+par_size.λ+2+4, :];\n",
    "        σ² = chain_θ_bound[par_size.R+par_size.d+par_size.Z+3+6+1, :];\n",
    "    end\n",
    "    \n",
    "    # spectral density\n",
    "    for draw=1:nDraws[2]-burnin[2]\n",
    "        s_distr[:, i, draw] = s(ω[:], λ[draw], ρ[draw], σ²[draw]);\n",
    "    end\n",
    "end\n",
    "\n",
    "# PC spectrum\n",
    "PCᵐ_s  = median(s_distr[:, 1, :], dims=2);\n",
    "PC95_s = zeros(size(PCᵐ_s));\n",
    "PC05_s = zeros(size(PCᵐ_s));\n",
    "PC84_s = zeros(size(PCᵐ_s));\n",
    "PC16_s = zeros(size(PCᵐ_s));\n",
    "\n",
    "EPᵐ_s  = median(s_distr[:, 2, :], dims=2);\n",
    "EP95_s = zeros(size(EPᵐ_s));\n",
    "EP05_s = zeros(size(EPᵐ_s));\n",
    "EP84_s = zeros(size(EPᵐ_s));\n",
    "EP16_s = zeros(size(EPᵐ_s));\n",
    "\n",
    "iCᵐ_s  = median(s_distr[:, 3, :], dims=2);\n",
    "iC95_s = zeros(size(iCᵐ_s));\n",
    "iC05_s = zeros(size(iCᵐ_s));\n",
    "iC84_s = zeros(size(iCᵐ_s));\n",
    "iC16_s = zeros(size(iCᵐ_s));\n",
    "\n",
    "for i=1:size(s_distr, 1)\n",
    "    PC95_s[i] = quantile(s_distr[i, 1, :], 0.95);\n",
    "    PC05_s[i] = quantile(s_distr[i, 1, :], 0.05);\n",
    "    PC84_s[i] = quantile(s_distr[i, 1, :], 0.84);\n",
    "    PC16_s[i] = quantile(s_distr[i, 1, :], 0.16);\n",
    "    \n",
    "    EP95_s[i] = quantile(s_distr[i, 2, :], 0.95);\n",
    "    EP05_s[i] = quantile(s_distr[i, 2, :], 0.05);\n",
    "    EP84_s[i] = quantile(s_distr[i, 2, :], 0.84);\n",
    "    EP16_s[i] = quantile(s_distr[i, 2, :], 0.16);\n",
    "    \n",
    "    iC95_s[i] = quantile(s_distr[i, 1, :], 0.95);\n",
    "    iC05_s[i] = quantile(s_distr[i, 1, :], 0.05);\n",
    "    iC84_s[i] = quantile(s_distr[i, 1, :], 0.84);\n",
    "    iC16_s[i] = quantile(s_distr[i, 1, :], 0.16);\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Chart -----\n",
    "\n",
    "figures=Array{Any}(undef, 4)\n",
    "\n",
    "titles_sub = [\"Business cycle\", \"Energy price cycle\", \"Business cycle: spectral density\", \"Energy price cycle: spectral density\"];\n",
    "\n",
    "for i=1:4\n",
    "    \n",
    "    if i == 1\n",
    "        layout  = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[1]));\n",
    "    elseif i == 2\n",
    "        layout  = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=scales[4]));\n",
    "        \n",
    "    else\n",
    "        layout  = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false, titlefont=attr(size=10), title=\"Frequency\"),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Spectral density\"));\n",
    "    end\n",
    "    \n",
    "    if i == 1\n",
    "        trace1  = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 1], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2  = pltjs.scatter(;x=date[1:end-max_h], y=PC95[1:end-max_h, 1], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=i==1);\n",
    "        trace3  = pltjs.scatter(;x=date[1:end-max_h], y=PC05[1:end-max_h, 1], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4  = pltjs.scatter(;x=date[1:end-max_h], y=PC84[1:end-max_h, 1], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=i==1);\n",
    "        trace5  = pltjs.scatter(;x=date[1:end-max_h], y=PC16[1:end-max_h, 1], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6a = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 1], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=i==1);\n",
    "        trace6b = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 1], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        trace7  = pltjs.scatter(;x=date[1:end-max_h], y=zeros(size(PCᵐ)[1]-max_h), name=\"\", mode=\"lines\", line=attr(width=1, color=\"black\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace6a; trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6b; trace7], layout);\n",
    "    elseif i == 2\n",
    "        trace1 = pltjs.scatter(;x=date[1:end-max_h], y=EPᵐ[1:end-max_h, 4], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=date[1:end-max_h], y=EP95[1:end-max_h, 4], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=date[1:end-max_h], y=EP05[1:end-max_h, 4], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=date[1:end-max_h], y=EP84[1:end-max_h, 4], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=date[1:end-max_h], y=EP16[1:end-max_h, 4], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=date[1:end-max_h], y=EPᵐ[1:end-max_h, 4], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        trace7 = pltjs.scatter(;x=date[1:end-max_h], y=zeros(size(EPᵐ)[1]-max_h), name=\"\", mode=\"lines\", line=attr(width=1, color=\"black\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6; trace7], layout);\n",
    "    elseif i == 3\n",
    "        trace1 = pltjs.scatter(;x=ω, y=PCᵐ_s[:], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=ω, y=PC95_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=ω, y=PC05_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=ω, y=PC84_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=ω, y=PC16_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=ω, y=PCᵐ_s[:], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6], layout);\n",
    "\n",
    "    else\n",
    "        trace1 = pltjs.scatter(;x=ω, y=EPᵐ_s[:], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=ω, y=EP95_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=ω, y=EP05_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=ω, y=EP84_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=ω, y=EP16_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=ω, y=EPᵐ_s[:], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6], layout);\n",
    "\n",
    "    end\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]];\n",
    "\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:4\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.165, x=0.33, font=attr(size=10), traceorder=\"normal\")\n",
    "\n",
    "savefig(fig, \"cycles_spectral.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ----- Chart -----\n",
    "\n",
    "figures=Array{Any}(undef, 4)\n",
    "\n",
    "titles_sub = [\"Business cycle\", \"Energy price cycle\", \"Business cycle: spectral density\", \"Energy price cycle: spectral density\"];\n",
    "\n",
    "for i=1:4\n",
    "    \n",
    "    if i == 1 || i == 2\n",
    "        layout  = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Percent\"));\n",
    "    else\n",
    "        layout  = pltjs.Layout(;title=titles_sub[i], titlefont=attr(size=12),\n",
    "                               xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false, titlefont=attr(size=10), title=\"Frequency\"),\n",
    "                               yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Spectral density\"));\n",
    "    end\n",
    "    \n",
    "    if i == 1\n",
    "        trace1  = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 5], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2  = pltjs.scatter(;x=date[1:end-max_h], y=PC95[1:end-max_h, 5], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=i==1);\n",
    "        trace3  = pltjs.scatter(;x=date[1:end-max_h], y=PC05[1:end-max_h, 5], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4  = pltjs.scatter(;x=date[1:end-max_h], y=PC84[1:end-max_h, 5], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=i==1);\n",
    "        trace5  = pltjs.scatter(;x=date[1:end-max_h], y=PC16[1:end-max_h, 5], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6a = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 5], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=i==1);\n",
    "        trace6b = pltjs.scatter(;x=date[1:end-max_h], y=PCᵐ[1:end-max_h, 5], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        trace7  = pltjs.scatter(;x=date[1:end-max_h], y=zeros(size(PCᵐ)[1]-max_h), name=\"\", mode=\"lines\", line=attr(width=1, color=\"black\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace6a; trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6b; trace7], layout);\n",
    "    elseif i == 2\n",
    "        trace1 = pltjs.scatter(;x=date[1:end-max_h], y=EPᵐ[1:end-max_h, 5], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=date[1:end-max_h], y=EP95[1:end-max_h, 5], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=date[1:end-max_h], y=EP05[1:end-max_h, 5], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=date[1:end-max_h], y=EP84[1:end-max_h, 5], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=date[1:end-max_h], y=EP16[1:end-max_h, 5], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=date[1:end-max_h], y=EPᵐ[1:end-max_h, 5], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        trace7 = pltjs.scatter(;x=date[1:end-max_h], y=zeros(size(EPᵐ)[1]-max_h), name=\"\", mode=\"lines\", line=attr(width=1, color=\"black\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6; trace7], layout);\n",
    "    elseif i == 3\n",
    "        trace1 = pltjs.scatter(;x=ω, y=PCᵐ_s[:], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=ω, y=PC95_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=ω, y=PC05_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=ω, y=PC84_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=ω, y=PC16_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=ω, y=PCᵐ_s[:], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6], layout);\n",
    "\n",
    "    else\n",
    "        trace1 = pltjs.scatter(;x=ω, y=EPᵐ_s[:], name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "        trace2 = pltjs.scatter(;x=ω, y=EP95_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace3 = pltjs.scatter(;x=ω, y=EP05_s[:], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace4 = pltjs.scatter(;x=ω, y=EP84_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace5 = pltjs.scatter(;x=ω, y=EP16_s[:], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "        trace6 = pltjs.scatter(;x=ω, y=EPᵐ_s[:], name=\"Median\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"), showlegend=false);\n",
    "        figures[i] = pltjs.plot([trace1; trace2; \n",
    "                                 trace1; trace3; \n",
    "                                 trace1; trace4;\n",
    "                                 trace1; trace5; \n",
    "                                 trace6], layout);\n",
    "\n",
    "    end\n",
    "end\n",
    "\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]];\n",
    "\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:4\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.165, x=0.33, font=attr(size=10), traceorder=\"normal\")\n",
    "\n",
    "savefig(fig, \"cycles_spectral_projection_infl.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Output gap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=1\n",
    "iC = distr_α[ind_cycles, :, :];\n",
    "iT = distr_α[ind_trends, :, :];\n",
    "output_gap1 = PC[i,1:end-max_h,:] + EP[i,1:end-max_h,:] + iC[i,1:end-max_h,:];\n",
    "output_gap1 = output_gap1 .* σʸ[i];\n",
    "output_gap1 = 100*(output_gap1 ./ (iT[i,1:end-max_h,:] .* σʸ[i]));\n",
    "\n",
    "OG95 = zeros(size(output_gap1,1));\n",
    "OG05 = zeros(size(output_gap1,1));\n",
    "OG50 = zeros(size(output_gap1,1));\n",
    "OG84 = zeros(size(output_gap1,1));\n",
    "OG16 = zeros(size(output_gap1,1));\n",
    "\n",
    "for j=1:size(output_gap1, 1)  \n",
    "    OG95[j, 1] = quantile(output_gap1[j,:], 0.95);\n",
    "    OG05[j, 1] = quantile(output_gap1[j,:], 0.05);\n",
    "    OG50[j, 1] = quantile(output_gap1[j,:], 0.50);\n",
    "    OG84[j, 1] = quantile(output_gap1[j,:], 0.84); \n",
    "    OG16[j, 1] = quantile(output_gap1[j,:], 0.16);\n",
    "end  \n",
    "\n",
    "# ----- Output gap vs CBO -----\n",
    "\n",
    "c1 = \"rgba(0, 0, 158, .7)\";\n",
    "c2 = \"rgba(255, 0, 0, .7)\";\n",
    "c3 = \"rgba(0, 0, 0, .7)\";\n",
    "\n",
    "i=1;\n",
    "\n",
    "gap_benchmark_file = CSV.read(\"gap.csv\") |> DataFrame;\n",
    "cbo = gap_benchmark_file[!,:CBO] |> Array{Union{Missing, Float64}};\n",
    "greenbook = gap_benchmark_file[!,:Greenbook] |> Array{Union{Missing, Float64}};\n",
    "greenbook[ismissing.(greenbook)] .= NaN;\n",
    "\n",
    "\n",
    "layout  = pltjs.Layout(;title=\"Output gap as a percentage of potential GDP\", titlefont=attr(size=12),\n",
    "                xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, nticks=20, tickangle=-90, zeroline=false),\n",
    "                yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"Percent\"));\n",
    "\n",
    "i=1;\n",
    "\n",
    "trace1 = pltjs.scatter(;x=date[1:end-max_h], y=OG50, name=\"\", mode=\"lines\", line=attr(color=\"rgba(0,0,0,0)\"), showlegend=false);\n",
    "trace2 = pltjs.scatter(;x=date[1:end-max_h], y=OG95, name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "trace3 = pltjs.scatter(;x=date[1:end-max_h], y=OG05, name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "trace4 = pltjs.scatter(;x=date[1:end-max_h], y=OG84, name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "trace5 = pltjs.scatter(;x=date[1:end-max_h], y=OG16, name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "trace6 = pltjs.scatter(;x=date[1:end-max_h], y=OG50, name=\"Trend-Cycle Model\", mode=\"lines\", line=attr(width=1.4, color=\"rgba(0,0,255,1)\"));\n",
    "trace7 = pltjs.scatter(;x=date[1:end-max_h], y=zeros(size(OG50)[1]), name=\"\", mode=\"lines\", line=attr(width=1, color=\"black\"), showlegend=false);\n",
    "trace8 = pltjs.scatter(x=date[1:end-max_h], y=cbo, name=\"CBO\", line=attr(width=1.4, color=\"rgb(255, 0, 0)\"));\n",
    "trace9 = pltjs.scatter(x=date[1:end-max_h], y=greenbook, name=\"Greenbook\", line=attr(width=1.4, color=\"rgb(11, 102, 35)\"));\n",
    "\n",
    "fig = pltjs.plot([trace1; trace2; \n",
    "                        trace1; trace3; \n",
    "                        trace1; trace4;\n",
    "                        trace1; trace5; \n",
    "                        trace6; trace7; trace8; trace9], layout);\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 300;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.25, x=0.3, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"output_gap.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Oil trend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figures = Array{Any}(undef, 1)\n",
    "\n",
    "trace0aa = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, 4], name=titles[4], mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=true)\n",
    "trace0a = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, 4], name=titles[4], mode=\"lines\", line=attr(width=1.4, color=\"black\"), showlegend=false)\n",
    "trace1a  = pltjs.scatter(;x=date[1:end-max_h], y=iT95[1:end-max_h, 4], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=true);\n",
    "trace2a  = pltjs.scatter(;x=date[1:end-max_h], y=iT05[1:end-max_h, 4], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false);\n",
    "trace3a  = pltjs.scatter(;x=date[1:end-max_h], y=iT84[1:end-max_h, 4], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=true);\n",
    "trace4a  = pltjs.scatter(;x=date[1:end-max_h], y=iT16[1:end-max_h, 4], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false);\n",
    "\n",
    "trace0bb = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, 5], name=titles[7], mode=\"lines\", line=attr(width=1.4, color=c1), showlegend=true, yaxis=\"y2\");\n",
    "trace0b = pltjs.scatter(;x=date[1:end-max_h], y=iTᵐ[1:end-max_h, 5], name=titles[7], mode=\"lines\", line=attr(width=1.4, color=c1), showlegend=false, yaxis=\"y2\");\n",
    "trace1b  = pltjs.scatter(;x=date[1:end-max_h], y=iT95[1:end-max_h, 5], name=\"CI, 90%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false, yaxis=\"y2\");\n",
    "trace2b  = pltjs.scatter(;x=date[1:end-max_h], y=iT05[1:end-max_h, 5], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .40)\", line_color=\"transparent\", showlegend=false, yaxis=\"y2\");\n",
    "trace3b  = pltjs.scatter(;x=date[1:end-max_h], y=iT84[1:end-max_h, 5], name=\"CI, 68%\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false, yaxis=\"y2\");\n",
    "trace4b  = pltjs.scatter(;x=date[1:end-max_h], y=iT16[1:end-max_h, 5], name=\"\", mode=\"lines\", fill=\"tonexty\", fillcolor=\"rgba(185, 185, 185, .70)\", line_color=\"transparent\", showlegend=false, yaxis=\"y2\");\n",
    "\n",
    "layout = pltjs.Layout(;title=string(\"Idiosyncratic trends\"), titlefont=attr(size=14), width=800, height=300,\n",
    "        xaxis=attr(tickfont_size=10, nticks=20, tickangle=-90, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false),\n",
    "        yaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, zeroline=false, titlefont=attr(size=10), title=\"\\$/Barrel\"),\n",
    "        yaxis2=attr(titlefont=attr(size=10), zeroline=false, tickfont_size=10, showgrid=false, title=\"Percent\", titleangle=90, overlaying=\"y\", side=\"right\"));\n",
    "\n",
    "fig = pltjs.plot([trace0aa; trace1a;\n",
    "                  trace0a; trace2a;\n",
    "                  trace0a; trace3a;\n",
    "                  trace0a; trace4a; \n",
    "                  trace0bb; trace1b;\n",
    "                  trace0b; trace2b;\n",
    "                  trace0b; trace3b;\n",
    "                  trace0b; trace4b], layout)\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Legend\n",
    "fig.plot.layout[\"legend\"] = attr(orientation=\"h\", y=-0.2, x=0.21, font=attr(size=10))\n",
    "\n",
    "savefig(fig, \"trends_idio_oil.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Slope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "function standardize(x)\n",
    "    return x.-mean(x)\n",
    "end    \n",
    "\n",
    "yy = standardize(PCᵐ[1:end-max_h, 5]);\n",
    "xx = standardize(PCᵐ[1:end-max_h, 3]);\n",
    "b1 = (xx'*xx)\\xx'*yy;\n",
    "z  = xx*b1;\n",
    "\n",
    "yy2 = standardize(data[1:end-max_h, 5]);\n",
    "xx2 = standardize(data[1:end-max_h, 3]);\n",
    "b2  = (xx2'*xx2)\\xx2'*yy2;\n",
    "z2  = xx2*b2;\n",
    "\n",
    "figures = Array{Any}(undef, 1)\n",
    "trace1a = pltjs.scatter(;x=xx[1:end-18], y=yy[1:end-18], mode=\"markers\", marker=attr(color=\"red\"), opacity=0.45, showlegend=false);\n",
    "trace1b = pltjs.scatter(;x=xx[end-17:end], y=yy[end-17:end], mode=\"markers\", marker=attr(color=\"red\", symbol=\"x\"), opacity=1, showlegend=false);\n",
    "trace2 = pltjs.scatter(;x=xx, y=z, showlegend=false, line=attr(color=\"darkred\"));\n",
    "trace3 = pltjs.scatter(;x=xx2, y=yy2, mode=\"markers\", marker=attr(color=\"blue\"), opacity=0.45, showlegend=false);\n",
    "trace4 = pltjs.scatter(;x=xx2, y=z2, showlegend=false, line=attr(color=\"blue\"));\n",
    "\n",
    "# Trace1b includes data from 2014 to 2018\n",
    "\n",
    "layout = pltjs.Layout(;titlefont=attr(size=14), width=800, height=300,\n",
    "        xaxis=attr(tickfont_size=10, showgrid=true, title=\"Unemployment Rate\"),\n",
    "        yaxis=attr(tickfont_size=10, showgrid=true, title=\"Headline Inflation\"))\n",
    "\n",
    "fig = pltjs.plot([trace1a; trace1b; trace2; trace3; trace4], layout)\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 800;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "savefig(fig, \"slope_cpi.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Regression output for global demand data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Save key output to csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OG=median((PC[1,:,:]+EP[1,:,:]+iC[1,:,:])./iT[1,:,:], dims=2)[:]*100;\n",
    "CSV.write(\"output_gap.csv\", DataFrame(timestamp=date, x=OG))\n",
    "CSV.write(\"PC.csv\", DataFrame(timestamp=date, x=PCᵐ[:,1]))\n",
    "CSV.write(\"EP.csv\", DataFrame(timestamp=date, x=EPᵐ[:,4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Appendix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Coefficients: loadings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c1 = \"rgba(0, 48, 158, .75)\";\n",
    "\n",
    "figures=Array{Any}(undef, 24)\n",
    "\n",
    "titles_coeff = [\"Coefficient for Employment, BC Cycle\",\n",
    "                \"Coefficient for URATE, BC Cycle\",\n",
    "                \"Coefficient for Oil, BC Cycle\",\n",
    "                \"Coefficient for Inflation, BC Cycle\",\n",
    "                \"Coefficient for Core Inflation, BC Cycle\",\n",
    "                \"Coefficient for UoM, BC Cycle\",\n",
    "                \"Coefficient for SPF, BC Cycle\",\n",
    "                \"Coefficient for Employment, aux BC Cycle\",\n",
    "                \"Coefficient for URATE, aux BC Cycle\",\n",
    "                \"Coefficient for Oil, aux BC Cycle\",\n",
    "                \"Coefficient for Inflation, aux BC Cycle\",\n",
    "                \"Coefficient for Core Inflation, aux BC Cycle\",\n",
    "                \"Coefficient for UoM, aux BC Cycle\",\n",
    "                \"Coefficient for SPF, aux BC Cycle\",\n",
    "                \"Coefficient for UoM, aux^2 BC Cycle\",\n",
    "                \"Coefficient for SPF, aux^2 BC Cycle\",\n",
    "                \"Coefficient for Inflation, EP Cycle\",\n",
    "                \"Coefficient for Core Inflation, EP Cycle\",   \n",
    "                \"Coefficient for UoM, EP Cycle\",\n",
    "                \"Coefficient for SPF, EP Cycle\",\n",
    "                \"Coefficient for Inflation, aux EP Cycle\",\n",
    "                \"Coefficient for Core Inflation, aux EP Cycle\",   \n",
    "                \"Coefficient for UoM, aux EP Cycle\",\n",
    "                \"Coefficient for SPF, aux EP Cycle\"];\n",
    "\n",
    "for i=1:24\n",
    "    trace1 = pltjs.histogram(x=chain_θ_bound[i,nDraws[2]-burnin[2]+1:nDraws[2]], opacity=0.75, showlegend=false, histnorm=\"probability\", nbinsx=45);\n",
    "    layout = pltjs.Layout(;title=titles_coeff[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, range =[-3, 3]),\n",
    "                           yaxis=attr(tickfont_size=10, nticks=10, tickmode=\"linear\", tick0=0, dtick=0.04, showgrid=true, range=[0,0.16], linecolor=\"black\", mirror=true, titlefont=attr(size=10), title=\"Relative frequency\"), range=[0, 0.14]);\n",
    "\n",
    "    figures[i] = pltjs.plot(trace1, layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1] figures[2] figures[3] figures[4]; figures[5] figures[6] figures[7] figures[8];\n",
    "       figures[9] figures[10] figures[11] figures[12]; figures[13] figures[14] figures[15] figures[16];\n",
    "       figures[17] figures[18] figures[19] figures[20]; figures[21] figures[22] figures[23] figures[24]]\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 1200;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:24\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "savefig(fig, \"appendix_Z_params.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Coefficients: drifts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = sum(par_ind.d) + sum(par_ind.Z) + sum(par_ind.R);\n",
    "\n",
    "figures=Array{Any}(undef, 2)\n",
    "\n",
    "titles_coeff = [\"Drift of idio trend, GDP\"; \"Drift of idio trend, Employment\"]  \n",
    "\n",
    "for i=1:2\n",
    "    trace1 = pltjs.histogram(x=chain_θ_bound[start+i,nDraws[2]-burnin[2]+1:nDraws[2]], opacity=0.75, showlegend=false, histnorm=\"probability\", nbinsx=45);\n",
    "    layout = pltjs.Layout(;title=titles_coeff[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true),\n",
    "                           yaxis=attr(tickfont_size=10, nticks=10, tickmode=\"linear\", tick0=0, dtick=0.02, showgrid=true, linecolor=\"black\", mirror=true, titlefont=attr(size=10), title=\"Relative frequency\"));\n",
    "\n",
    "    figures[i] = pltjs.plot(trace1, layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1]; figures[2]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 400;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:2\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "savefig(fig, \"appendix_c_params.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Coefficients: shock variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "start = sum(par_ind.d) + sum(par_ind.Z) + sum(par_ind.R) + sum(par_ind.c);\n",
    "\n",
    "figures=Array{Any}(undef, 18)\n",
    "\n",
    "titles_coeff = [\"Shock variance for BC Cycle\",\n",
    "                \"Shock variance for EP Cycle\",\n",
    "                \"Shock variance for Common Trend\",\n",
    "                \"Shock variance for idio cycle, GDP\",\n",
    "                \"Shock variance for idio trend, GDP\",  \n",
    "                \"Shock variance for idio cycle, Employment\",\n",
    "                \"Shock variance for idio trend, Employment\",  \n",
    "                \"Shock variance for idio cycle, URATE\",\n",
    "                \"Shock variance for idio trend, URATE\",\n",
    "                \"Shock variance for idio cycle, Oil price\",\n",
    "                \"Shock variance for idio trend, Oil price\",     \n",
    "                \"Shock variance for idio cycle, Inflation\",\n",
    "                \"Shock variance for idio cycle, Core inflation\",\n",
    "                \"Shock variance for idio cycle, SPF\",\n",
    "                \"Shock variance for idio trend, SPF\",\n",
    "                \"Shock variance for idio cycle, UoM\",\n",
    "                \"Shock variance for idio trend, UoM\"];\n",
    "\n",
    "for i=1:17\n",
    "    trace1 = pltjs.histogram(x=chain_θ_bound[start+i,nDraws[2]-burnin[2]+1:nDraws[2]], opacity=0.75, showlegend=false, histnorm=\"probability\", nbinsx=45);\n",
    "    layout = pltjs.Layout(;title=titles_coeff[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true),\n",
    "                           yaxis=attr(tickfont_size=10, nticks=10, tickmode=\"linear\", tick0=0, dtick=0.04, showgrid=true, range=[0, 0.14], linecolor=\"black\", mirror=true, titlefont=attr(size=10), title=\"Relative frequency\"));\n",
    "\n",
    "    figures[i] = pltjs.plot(trace1, layout);\n",
    "end\n",
    "figures[18] = pltjs.plot();\n",
    "\n",
    "fig = [figures[1] figures[2] figures[3]; figures[4] figures[5] figures[6]; figures[7] figures[8] figures[9]; figures[10] figures[11] figures[12]; figures[13] figures[14] figures[15]; figures[16] figures[17] figures[18]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 1000;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:17\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "savefig(fig, \"appendix_Q_params.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Coefficients: frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = par_size.d+par_size.Z+par_size.R+par_size.c+par_size.T+par_size.Q;\n",
    "\n",
    "figures=Array{Any}(undef, 10)\n",
    "\n",
    "titles_coeff = [\"Frequency of BC Cycle\",\n",
    "                \"Frequency of EP Cycle\",\n",
    "                \"Frequency of idio Cycle, GDP\",\n",
    "                \"Frequency of idio Cycle, Employment\",\n",
    "                \"Frequency of idio Cycle, URATE Rate\",\n",
    "                \"Frequency of idio Cycle, Oil price\",\n",
    "                \"Frequency of idio Cycle, Inflation\",\n",
    "                \"Frequency of idio Cycle, Core inflation\",\n",
    "                \"Frequency of idio Cycle, UoM\",\n",
    "                \"Frequency of idio Cycle, SPF\"]  \n",
    "\n",
    "for i=1:10\n",
    "    trace1 = pltjs.histogram(x=chain_θ_bound[start+i,nDraws[2]-burnin[2]+1:nDraws[2]], opacity=0.75, showlegend=false, histnorm=\"probability\", nbinsx=45);\n",
    "    layout = pltjs.Layout(;title=titles_coeff[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, range=[0,3.14]),\n",
    "                           yaxis=attr(tickfont_size=10, nticks=10, tickmode=\"linear\", tick0=0, dtick=0.04, showgrid=true, range=[0,0.16], linecolor=\"black\", mirror=true, titlefont=attr(size=10), title=\"Relative frequency\"));\n",
    "\n",
    "    figures[i] = pltjs.plot(trace1, layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]; figures[5] figures[6]; figures[7] figures[8]; figures[9] figures[10]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:10\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "savefig(fig, \"appendix_lambda_params.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "start = par_size.d+par_size.Z+par_size.R+par_size.c+par_size.T+par_size.Q + par_size.λ;\n",
    "\n",
    "figures=Array{Any}(undef, 10)\n",
    "\n",
    "titles_coeff = [\"Persistence of BC Cycle\",\n",
    "                \"Persistence of EP Cycle\",\n",
    "                \"Persistence of idio Cycle, GDP\",\n",
    "                \"Persistence of idio Cycle, Employment\",\n",
    "                \"Persistence of idio Cycle, URATE Rate\",\n",
    "                \"Persistence of idio Cycle, Oil price\",\n",
    "                \"Persistence of idio Cycle, Inflation\",\n",
    "                \"Persistence of idio Cycle, Core inflation\",\n",
    "                \"Persistence of idio Cycle, UoM\",\n",
    "                \"Persistence of idio Cycle, SPF\"]  \n",
    "\n",
    "for i=1:10\n",
    "    trace1 = pltjs.histogram(x=chain_θ_bound[start+i,nDraws[2]-burnin[2]+1:nDraws[2]], opacity=0.75, showlegend=false, histnorm=\"probability\", nbinsx=45);\n",
    "    layout = pltjs.Layout(;title=titles_coeff[i], titlefont=attr(size=12),\n",
    "                           xaxis=attr(tickfont_size=10, showgrid=true, linecolor=\"black\", mirror=true, range=[0,1]),\n",
    "                           yaxis=attr(tickfont_size=10, nticks=10, tickmode=\"linear\", tick0=0, dtick=0.04, showgrid=true, range=[0,0.16], linecolor=\"black\", mirror=true, titlefont=attr(size=10), title=\"Relative frequency\"));\n",
    "\n",
    "    figures[i] = pltjs.plot(trace1, layout);\n",
    "end\n",
    "\n",
    "fig = [figures[1] figures[2]; figures[3] figures[4]; figures[5] figures[6]; figures[7] figures[8]; figures[9] figures[10]];\n",
    "\n",
    "# Size\n",
    "fig.plot.layout[\"width\"]  = 800;\n",
    "fig.plot.layout[\"height\"] = 600;\n",
    "\n",
    "# Bars\n",
    "fig.plot.layout[\"bargap\"]      = 0;\n",
    "fig.plot.layout[\"bargroupgap\"] = 0;\n",
    "\n",
    "# Margins\n",
    "fig.plot.layout[\"margin\"][:b]  = 40;\n",
    "fig.plot.layout[\"margin\"][:t]  = 40;\n",
    "fig.plot.layout[\"margin\"][:r]  = 40;\n",
    "fig.plot.layout[\"margin\"][:l]  = 40;\n",
    "\n",
    "# Title size\n",
    "for i=1:10\n",
    "    fig.plot.layout[\"annotations\"][i][:font][:size] = 12;\n",
    "end\n",
    "\n",
    "savefig(fig, \"appendix_rho_params.pdf\", format=\"pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
